package com.ctgu.rabbitmq.rabbitmq.queue.maxLength;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName MaxLengthConfig
 * @ Author Alex2
 * @ Date 2025/8/15 10:22
 **/
@Configuration
public class MaxLengthConfig {
    // 正常交换机
    public static final String NORMAL_EXCHANGE_MAX = "normal_exchange_max";
    public static final String NORMAL_QUEUE_MAX = "normal_queue_max";
    public static final String NORMAL_ROUTING_KEY_MAX = "normal_routing_key_max";

    // 死信交换机
    public static final String DLX_EXCHANGE_MAX = "dlx_exchange_max";
    public static final String DLX_QUEUE_MAX = "dlx_queue_max";
    public static final String DLX_ROUTING_KEY_MAX = "dlx_routing_key_max";

    @Bean
    public DirectExchange normalExchangeMax() {
        return new DirectExchange(NORMAL_EXCHANGE_MAX);
    }

    @Bean
    public DirectExchange dlxExchangeMax() {
        return new DirectExchange(DLX_EXCHANGE_MAX);
    }

    // 绑定死信交换机
    @Bean
    public Queue normalQueueMax() {
        return QueueBuilder.nonDurable(NORMAL_QUEUE_MAX)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE_MAX)
                .withArgument("x-dead-letter-routing-key", DLX_ROUTING_KEY_MAX)
                .withArgument("x-max-length", 5)            // 队列最大长度
                .build();
    }

    @Bean
    public Queue dlxQueueMax() {
        return new Queue(DLX_QUEUE_MAX, false);
    }

    @Bean
    public Binding bindingNormalQueueMax() {
        return BindingBuilder.bind(normalQueueMax())
                .to(normalExchangeMax())
                .with(NORMAL_ROUTING_KEY_MAX);
    }

    @Bean
    public Binding bindingDlxQueueMax() {
        return BindingBuilder.bind(dlxQueueMax())
                .to(dlxExchangeMax())
                .with(DLX_ROUTING_KEY_MAX);
    }
}